<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Invoices extends CI_Controller
{

	var $data;

	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('tank_auth');
		$this->data=array();
	}

	function _sendData(){
		$this->output->set_header("Content-type: application/json");
		$this->output->set_output(json_encode($this->data));
	}

	function _remap($method, $params = array())
	{
		$this->load->model('invoicemodel');
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		} else {
			$this->profiles=$this->tank_auth->get_profile();
			$isOwner=false;
			foreach($this->profiles as $row){
				if($row->type==="Owner"){
					$isOwner=true;
				}
			}
			if ($isOwner) {
				if (method_exists($this, $method))
				{
					return call_user_func_array(array($this, $method), $params);
				}
			}
		}
		show_404(); // Either means that the user isn't authorized or that the page doesn't exist.
	}

	function index()
	{
		show_404();
	}

	function get($oid=false){
		if(!$oid){
			$oid=$this->input->get_post('oid');
		}
		if($oid){ // Return a single Invoice object.
			$this->data['data'] = $this->invoicemodel->getByInvoiceId($oid);
			$this->_sendData();
		} else { // Return all Invoice objects for the logged-in Owner.
			$page = 1; // The current page
			$sortname = 'invoice_id'; // Sort column
			$sortorder = 'asc'; // Sort order
			$qtype = ''; // Search column
			$query = ''; // Search string
			$rp = 15; // Rows per page
			if($this->input->get_post('page')){
				$page=$this->input->get_post('page');
			}
			if($this->input->get_post('sortname')){
				$sortname=$this->input->get_post('sortname');
			}
			if($this->input->get_post('sortorder')){
				$sortorder=$this->input->get_post('sortorder');
			}
			if($this->input->get_post('qtype')){
				$qtype=$this->input->get_post('qtype');
			}
			if($this->input->get_post('query')){
				$query=$this->input->get_post('query');
			}
			if($this->input->get_post('rp')){
				$rp=$this->input->get_post('rp');
			}
			$this->data['page']=$page;
			$this->data['total']=$this->invoicemodel->getCountByOwnerId($this->tank_auth->get_user_id(),$qtype,$query);
			$this->data['rows']=array();
			$queryData = $this->invoicemodel->getAllByOwnerId($this->tank_auth->get_user_id(),$sortname,$sortorder,$qtype,$query,$page,$rp);
			
			foreach($queryData as $row){
				$row = $this->invoicemodel->processInvoice($row);
				$this->data['rows'][]=array(
					'id'=>$row->invoice_id,
					'cell'=>$row
				);
			}
			$this->_sendData();
		}
	}

	function remove_item($oid=false){
		if(!$oid){
			$oid=$this->input->get_post('oid');
		}
		if($oid){
			if($oid>=0){
				$this->invoicemodel->removeItemById($oid);
			}
			$this->_sendData();
		}
	}

	function save($act='add',$oid=false){
		try{
			$act=$this->input->get_post('act',false,$act);
			$data = array();
			$data['owner_id'] = $this->tank_auth->get_user_id();
			$data['client_id'] = $this->input->get_post('client_id',false,'');
			$data['mailing_address'] = $this->input->get_post('address',false,'');
			if($this->input->get_post('issue_date')){
				$data['issue_date']=$this->input->get_post('issue_date');
			} else {
				$data['issue_date'] = date('Y-m-d');
			}
			$data['discount'] = $this->input->get_post('discount',false,'0.00');
			$data['taxes'] = $this->input->get_post('taxes',false,'0.00');
			$data['paid'] = $this->input->get_post('paid',false,'0.00');
			$data['terms'] = $this->input->get_post('terms',false,'');
			$data['notes'] = $this->input->get_post('notes',false,'');
		
			$new_items=$this->input->get_post('new_item',false,array());
			if($act==='add'){
				$oid = $this->invoicemodel->insertInvoice($data,$new_items);
			} else if($act === 'mod'){
				if(!$oid){
					$oid = $this->input->get_post('invoice_id');
				}
				if($oid){
					$old_items=$this->input->get_post('item',false,array());
					$this->invoicemodel->updateInvoice($oid,$data,$old_items,$new_items);
				}
			}
			$this->data['status']='success';
			$this->data['msg'] = 'Invoice '.$oid.' successfully saved!';
		} catch (Exception $x) {
			$this->data['status']='fail';
			$this->data['msg'] = $x->getMessage();
			$this->data['exception']=$x;
			log_message('error',$x);
		}
		$this->_sendData();
	}

}

